import 'package:flutter/material.dart';
import 'package:logger/logger.dart';

class AppLifecycle extends StatefulWidget {
  const AppLifecycle({super.key});

  @override
  State<AppLifecycle> createState() => _AppLifecycleState();
}

class _AppLifecycleState extends State<AppLifecycle> with WidgetsBindingObserver {
  @override
  void initState() {
    WidgetsBinding.instance.addObserver(this);
    // TODO: implement initState
    super.initState();
    Logger().i('initState');
  }

  @override
  Widget build(BuildContext context) {
    Logger().i('build');
    return Scaffold(
      appBar: AppBar(title: Text("应用生命周期")),
      body: Container(
        child: Text("应用生命周期"),
      ),
    );
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    // TODO: implement didChangeAppLifecycleState
    super.didChangeAppLifecycleState(state);

    if (state == AppLifecycleState.paused) {
      Logger().i('paused');
    } else if (state == AppLifecycleState.resumed) {
      Logger().i('resumed');
    } else if (state == AppLifecycleState.detached) {
      Logger().i('detached');
    } else if (state == AppLifecycleState.hidden) {
      Logger().i('hidden');
    } else if (state == AppLifecycleState.inactive) {
      Logger().i('inactive  例如：来电话');
    }
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    WidgetsBinding.instance.removeObserver(this);
  }
}
